<?php
namespace Admin\Controller;
use Think\Controller;

class IndexController extends BaseController {
	
    public function index(){
    	// $auth_url = "http://auth.164pk.com/Home/Index/authcheck?auth_code=".C('auth_code');
    	// $auth = curlGet($auth_url);
    	// $auth = trim($auth,chr(239).chr(187).chr(191));
    	// $this->assign('auth',json_decode($auth,true));

    	//上分请求个数

    	$xf_count = M('fenxia')->where("status=0")->count();
    	$sf_count = M('fenadd')->where("`check`=0")->count();
    	$is_mobile = is_mobile();
		

    	$userinfo = session('admin');

		//print_r($online_user);
    	
		$this->assign("is_mobile",$is_mobile);
    	$this->assign("userinfo",$userinfo);
    	$this->assign("sf_count",$sf_count);
    	$this->assign("xf_count",$xf_count);
        $this->display();
	}

	public function show(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}

    	$start=mktime(0,0,0,date('m'),date('d'),date('Y'));
		$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
		

		$map['time'] = array(array('egt',$start),array('elt',$end),'and');
		$map['state'] = 1;
		$map['is_robot'] = 0;
		$order = M('order');

		$pt_today = $order->field("SUM(add_points) AS add_points,SUM(del_points) AS del_points")->where($map)->find();
		$data['today_ying'] = $pt_today['del_points'] - $pt_today['add_points'];

		//平台近一个月输赢
		$start_m=mktime(0,0,0,date('m'),date('d')-29,date('Y'));
		$end_m=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
		$map_m['time'] = array(array('egt',$start_m),array('elt',$end_m),'and');
		$map_m['state'] = 1;
		$map_m['is_robot'] = 0;
		$pt_all = $order->field("SUM(add_points) AS add_points,SUM(del_points) AS del_points")->where($map_m)->find();
		$data['all_ying'] = $pt_all['del_points'] - $pt_all['add_points'];

		//今日新增用户
		$start_u=mktime(0,0,0,date('m'),date('d'),date('Y'));
		$end_u=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;

		$map_u['reg_time'] = array(array('egt',$start_u),array('elt',$end_u),'and');
		
		$user = M('user');
		$data['today_user'] = $user->where($map_u)->count();

		//最近一个月新增用户
		$start_u_m=mktime(0,0,0,date('m'),date('d')-29,date('Y'));
		$end_u_m=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
		$map_u_m['reg_time'] = array(array('egt',$start_u_m),array('elt',$end_u_m),'and');
		
		$user = M('user');
		$data['month_user'] = $user->where($map_u_m)->count();

		//今日上分，进入下分，总上下分
		$map_fen['time'] = array(array('egt',$start),array('elt',$end),'and');
		// var_dump($map_fen);

		$fen_add = M('integral')->field("sum(points) as sum_add")->where("type=1 and is_robot = 0")->where($map_fen)->find();
		$fen_xia = M('integral')->field("sum(points) as sum_xia")->where("type=0 and is_robot = 0")->where($map_fen)->find();
		$data['today_addfen'] = $fen_add['sum_add']?:0;
		$data['today_xiafen'] = $fen_xia['sum_xia']?:0;

		//总上下分
		$fen_sumadd = M('integral')->field("sum(points) as sum_add")->where("type=1 and is_robot=0")->find();
		$fen_sumxia = M('integral')->field("sum(points) as sum_xia")->where("type=0 and is_robot=0")->find();

		$data['all_fenadd'] = $fen_sumadd['sum_add']?:0;
		$data['all_fenxia'] = $fen_sumxia['sum_xia']?:0;
		$online_user=M('user')->where('user_login=1')->select();
		$this->assign("online_user",$online_user);
		$this->assign("front_url",$front_url);
		$this->assign("admin_url",$admin_url);
		$this->assign("agent_url",$agent_url);
		$this->assign("data",$data);
		$this->assign("auth",$auth);
		$this->display();
	}


	public function short_url(){
		$type = I('type');




        //$apiurl='http://api.suolink.cn/api.php';
        $apiurl='http://h5ip.cn/index/api';
		if($type==1){

           // http://h5ip.cn/index/api?url=http%3a%2f%2fwww.baidu.com

//            //请求：http://api.suolink.cn/api.php?url=urlencode('http://www.baidu.com')&key=5c0f89c0042c2@55f3a49150625ab&expireDate=2019-03-31

            //$res=$apiurl.'?url=urlencode(\''.$_SERVER['SERVER_NAME'].'\')&key=5cbeb9878e676d197e2eb293@57b29706949076611b2dedce2456455d&expireDate='.date('Y-m-d');
            $res=$apiurl.'?url=http://'.$_SERVER['SERVER_NAME'];
            $r=curlGet($res);
            echo $r;exit;



        }
//
//		$auth_url = "http://auth.httx06.cn/Home/Index/authcheck?auth_code=9STWmr4ENbk0dQalLpRIN12N93tf5KMQ";
//		$auth = curlGet($auth_url);
//		$auth = trim($auth,chr(239).chr(187).chr(191));
//		$auth = json_decode($auth,true);
//		if (!$auth) {
//			$this->error();
//		}
//		if ($type == 1) {
//			$s_url = "http://".$auth['site_url']."/Home/get/go?auth_code=".C('auth_code');
//			$longurl = "http://api.u6.gg/api.php?format=json&url=".urlencode($s_url);
//			// $longurl = "http://suo.im/api.php?format=json&url=".urlencode($s_url);
//			$r=curlGet($longurl);
//			$r = substr($r,10);
//			$items = json_decode("{".$r);
//
//			$front_url = $items->url;
//			echo date('Y-m-d');
//			echo $front_url;exit;
//		}
//		if ($type == 2) {
//			$s_url = "http://".$auth['site_url']."/Home/get/go_admin?auth_code=".C('auth_code');
//			// $longurl = "http://suo.im/api.php?format=json&url=".urlencode($s_url);
//			$longurl = "http://api.u6.gg/api.php?format=json&url=".urlencode($s_url);
//			// $longurl = "http://suo.im/api.php?format=json&url=".urlencode($s_url);
//			$r=curlGet($longurl);
//			$r = substr($r,10);
//			$items = json_decode("{".$r);
//			$front_url = $items->url;
//			echo $front_url;exit;
//		}
//		if ($type == 3) {
//			$s_url = "http://".$auth['site_url']."/Home/get/go_agent?auth_code=".C('auth_code');
//			// $longurl = "http://suo.im/api.php?format=json&url=".urlencode($s_url);
//			$longurl = "http://api.u6.gg/api.php?format=json&url=".urlencode($s_url);
//			// $longurl = "http://suo.im/api.php?format=json&url=".urlencode($s_url);
//			$r=curlGet($longurl);
//			$r = substr($r,10);
//			$items = json_decode("{".$r);
//			$front_url = $items->url;
//			echo $front_url;exit;
//		}
	}
		
	public function main(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}
		
		$number = M('number');
		$count = $number->count();
		$page = new \Think\Page($count,10);
		$show = $page->show();
		$list = $number->limit($page->firstRow.','.$page->listRows)->order("id DESC")->select();
		for($i=0;$i<count($list);$i++){
			$list[$i]['order'] = M('order')->where("number = {$list[$i]['']}")->select();
		}
		$this->display();
	}
	
	public function pwd() {
		$User = M('admin');
		$user2 = session('admin');
		if ($_POST) {
			if (!IS_AJAX) {
				$this->error('提交方式不正确', U('index/pwd'), 0);
			} else {
				$data['user'] = I('post.user');
				$data['password'] = md5(I('post.oldpassword'));
				$newpassword = md5(I('post.newpassword'));
				$repassword = md5(I('post.repassword'));
				$result = $User->where($data)->find();

				if ($result) {
					if ($newpassword != $repassword) {
						$this->error("两次输入新密码不一致");
					} else {
						$User->where($data)->setField('password', $newpassword);
						$this->success("修改成功", U('Login/index'),1);
					}
				} else {
					$this->error("账号或密码不正确");
				}
			}
		}
		$this -> assign('user2', $user2);
		$this -> display();
	}

	public function del() {
		delFileByDir(APP_PATH.'Runtime/');
		$this->success('删除缓存成功！',U('Admin/Index/index'));
	}	

		//用户返水
	public function kongzhi(){
		$kongzhi = M('kongzhi');
		$count = $kongzhi->count();
		$page = new \Think\Page($count,100);
		$show = $page->show();
		$list = $kongzhi->limit($page->firstRow.','.$page->listRows)->order("id desc")->select();


		$this->assign('show',$show);
		$this->assign('list',$list);

		$this->display();
	}



	/**
	 * bj数据下载
	 */
	public function kongzhi_actionbj28xz(){
		
		$ua = $_SERVER["HTTP_USER_AGENT"];
    $filename = "mybj28.txt";
    $encoded_filename = urlencode($filename);
    $encoded_filename = str_replace("+","%20",$encoded_filename);
    //第二步：生成TXT文件
    header("Content-Type: application/octet-stream");
    if (preg_match("/MSIE/",$_SERVER['HTTP_USER_AGENT'])){
        header('Content-Disposition:attachment;filename="'.$encoded_filename.'"');
    }elseif(preg_match("/Firefox/",$_SERVER['HTTP_USER_AGENT'])){
        header('Content-Disposition:attachment;filename*="utf8'.$filename.'"');
    }else{
        header('Content-Disposition:attachment;filename="'.$filename.'"');
    }
	//第三步：输出内容
	$str= file_get_contents($filename);
    echo $str;
    /*file_put_contents($filename,json_encode($data),FILE_APPEND);
    echo file_get_contents("$filename");*/
	}
	
	/**
	 * jnd数据下载
	 */
	public function kongzhi_actionjnd28xz(){
		
		$ua = $_SERVER["HTTP_USER_AGENT"];
    $filename = "myjnd28.txt";
    $encoded_filename = urlencode($filename);
    $encoded_filename = str_replace("+","%20",$encoded_filename);
    //第二步：生成TXT文件
    header("Content-Type: application/octet-stream");
    if (preg_match("/MSIE/",$_SERVER['HTTP_USER_AGENT'])){
        header('Content-Disposition:attachment;filename="'.$encoded_filename.'"');
    }elseif(preg_match("/Firefox/",$_SERVER['HTTP_USER_AGENT'])){
        header('Content-Disposition:attachment;filename*="utf8'.$filename.'"');
    }else{
        header('Content-Disposition:attachment;filename="'.$filename.'"');
    }
	//第三步：输出内容
	$str= file_get_contents($filename);
    echo $str;
    /*file_put_contents($filename,json_encode($data),FILE_APPEND);
    echo file_get_contents("$filename");*/
	}
	

	/**
	 * bj批量添加
	 */
	public function kongzhi_actionbj28(){

	

		if(file_exists("mybj28.txt")){

			if(unlink("mybj28.txt")){
				echo '处理中<br>';
				
				
			}else{
				echo '存在读写权限不足，请配置好权限， 或者手动删除mybj28.txt<br>';
				exit;
			}
		}
		echo '北京28数据生成中,请勿关闭页面<br>';
		//exit;

		$resqs = M('number')->where("game='bj28'")->order('id desc')->limit(1)->find();
		M('kongzhi')->where("game='bj28'")->delete();

		// dump($resqs['periodnumber']);

	//	$sqqs=$resqs['periodnumber'];//上期期数
	//	$sqqs=$resqs['periodnumber']+1;//当前期数

	//	$sqqs=$resqs['periodnumber']+2;//下期数

		$sqqs=$resqs['periodnumber']+1;//下期数
		

		$zksjd=1000;
		

		$scs=0;
		for ($i=1; $i <=$zksjd; $i++) { 
			$numa=mt_rand(0, 9);
			$numb=mt_rand(0, 9);
			$numc=mt_rand(0, 9);
			//echo '期数：'.$i.'开奖号是：'.$numa.','.$numb.','.$numc.'<br>';

				//$sqqs=$sqqs+$i;

				$xq=$sqqs+$i;

				$data['game'] = 'bj28';
				$data['periodnumber'] =$xq ;
				$data['awardnumbers'] = $numa.','.$numb.','.$numc;
				$data['addtime'] = time();

				$res = M('kongzhi')->add($data);
				if($res){
					$scs=$scs+1;

					

					
					file_put_contents("mybj28.txt", '期数：'.$xq.' 开奖号是：'.$numa.','.$numb.','.$numc.PHP_EOL, FILE_APPEND);
					if($scs==$zksjd){
						echo $scs.'期生成完成';
						$this->success('北京28生成成功',U('Admin/Index/kongzhi'));
					}

				}


		}

		//echo $numa.$numb.$numc;
	}
	
	public function kongzhi_actionjnd28(){
		if(file_exists("myjnd28.txt")){

			if(unlink("myjnd28.txt")){
				echo '处理中<br>';
				
				
			}else{
				echo '存在读写权限不足，请配置好权限， 或者手动删除myjnd28.txt<br>';
				exit;
			}
		}
		echo '加拿大28数据生成中,请勿关闭页面<br>';
		//exit;

		$resqs = M('number')->where("game='jnd28'")->order('id desc')->limit(1)->find();
		M('kongzhi')->where("game='jnd28'")->delete();

		// dump($resqs['periodnumber']);

	//	$sqqs=$resqs['periodnumber'];//上期期数
	//	$sqqs=$resqs['periodnumber']+1;//当前期数

	//	$sqqs=$resqs['periodnumber']+2;//下期数

		$sqqs=$resqs['periodnumber']+1;//下期数
		

		$zksjd=1000;
		

		$scs=0;
		for ($i=1; $i <=$zksjd; $i++) { 
			$numa=mt_rand(0, 9);
			$numb=mt_rand(0, 9);
			$numc=mt_rand(0, 9);
			//echo '期数：'.$i.'开奖号是：'.$numa.','.$numb.','.$numc.'<br>';

				//$sqqs=$sqqs+$i;

				$xq=$sqqs+$i;

				$data['game'] = 'jnd28';
				$data['periodnumber'] =$xq ;
				$data['awardnumbers'] = $numa.','.$numb.','.$numc;
				$data['addtime'] = time();

				$res = M('kongzhi')->add($data);
				if($res){
					$scs=$scs+1;

					

					
					file_put_contents("myjnd28.txt", '期数：'.$xq.' 开奖号是：'.$numa.','.$numb.','.$numc.PHP_EOL, FILE_APPEND);
					if($scs==$zksjd){
						echo $scs.'期生成完成';
						$this->success('加拿大28生成成功',U('Admin/Index/kongzhi'));
					}

				}


		}


		
	}

	public function kongzhi_action(){
		$data = I();
		if (!$data['periodnumber']) {
			$this->error('期号错误');
		}
		$aw_len = count(explode(',', $data['awardnumbers']));
		if ($data['game'] == 'pk10' || $data['game'] == 'xyft') {
			if ($aw_len != '10') {
				$this->error("添加开奖号码格式错误，请检查");
			}
			$new_arr = array();
			foreach (explode(',', $data['awardnumbers']) as $value) {
				if ($value > 10 || $value <= 0) {
					$this->error("添加开奖号码格式错误，号码只能到1-10");
				}
				if (strlen($value) == 1) {
					$new_arr[] = '0'.$value;
				}else{
					$new_arr[] = $value;
				}
			}
			$data['awardnumbers'] = implode(',', $new_arr);
		}
		// var_dump($data);exit;
		if ($data['game'] == 'ssc') {
			if ($aw_len != '5') {
				$this->error("添加开奖号码格式错误，请检查");
			}
			foreach (explode(',', $data['awardnumbers']) as $value) {
				if ($value > 10 || $value <= 0 || strlen($value) > 1) {
					$this->error("添加开奖号码格式错误，号码只能到1-10,号码前面不加0");
				}
			}
		}
		if ($data['game'] == 'bj28' || $data['game'] == 'jnd28' || $data['game'] == 'k3' || $data['game'] == 'k3bj' || $data['game'] == 'k3gx' || $data['game'] == 'k3jl' || $data['game'] == 'k3ah' || $data['game'] == 'k3fj') {
			if ($aw_len != '3') {
				$this->error("添加开奖号码格式错误，请检查");
			}
			foreach (explode(',', $data['awardnumbers']) as $value) {
				if ($value > 10 || $value <= 0 || strlen($value) > 1) {
					$this->error("添加开奖号码格式错误，号码只能到1-10,号码前面不加0");
				}
			}
		}

		$data['addtime'] = time();


		$res = M('kongzhi')->add($data);

		if ($res) {
			$this->success('添加成功',U('Admin/Index/kongzhi'));
		}else{
			$this->success('添加失败');
		}
	}

	
	
		
}